package edu.neu.highway.controller;

import edu.neu.highway.annotation.IgnoreResponseAdvice;
import edu.neu.highway.neo4j.CypherExecutor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <h1>项目测试接口</h1>
 */
@RestController
@Api(tags = "测试系统")
public class TestController {

    private final CypherExecutor cypherExecutor;

    public TestController(CypherExecutor cypherExecutor) {
        this.cypherExecutor = cypherExecutor;
    }

    @GetMapping("/hello")
    @ApiOperation(httpMethod = "GET", value = "测试系统接口", notes = "返回hello world")
    public List<String> sayHello() {
        List<String> res = new ArrayList<>();
        res.add("Hello World");
        return res;
    }

    @GetMapping("/hello-without-advice")
    @ApiOperation(httpMethod = "GET", value ="无结果返回接口", notes = "返回不带有包装的串")
    @IgnoreResponseAdvice
    public String sayHelloWithoutR() {
        return "Hello World Pure";
    }

    @GetMapping("/hello-with-exception")
    @ApiOperation(httpMethod = "GET", value = "异常返回结果", notes = "系统异常处理测试")
    public String sayHelloWithException() {
        throw new RuntimeException("Hello Exception");
    }

    @GetMapping("/ping-from-neo4j")
    @ApiOperation(httpMethod = "GET", value = "Neo4j数据库链接测试", notes = "数据库链接测试")
    public List<String> pingFromNeo4j() {
        return cypherExecutor.readCyphers("MATCH (a) return a", record -> {
            if (record.get(0).asNode().hasLabel("Movie")) {
                return record.get(0).asNode().get("title").toString();
            } else if (record.get(0).asNode().hasLabel("Person")) {
                return record.get(0).asNode().get("name").toString();
            } else {
                return "unknown node";
            }
        });
    }
}
